package com.zhou.framework.model;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.collections.MapUtils;

import javax.validation.constraints.Min;
import javax.validation.constraints.Positive;
import java.util.Map;

/**
 * @author lang.zhou
 * @date 2020/1/21
 */
@Data
@ApiModel("分页参数模型")
public class PageParam{

    @ApiModelProperty("当前页数")
    @Positive
    private Integer pageNum = 1;

    @ApiModelProperty("每页显示数量")
    @Positive
    private Integer pageSize = 10;

    @ApiModelProperty("排序字段名")
    private String orderColumn;

    @ApiModelProperty("排序类型")
    private String orderType;

    public Integer getFrom(){
        return (pageNum - 1) * pageSize;
    }
    public Integer getTo(){
        return getFrom() + pageSize;
    }
    public Integer getOffset(){
        return getFrom();
    }
    public Integer getLimit(){
        return pageSize;
    }

    public PageParam(Integer pageNum,Integer pageSize) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
    }

    public PageParam() {
    }
    public PageParam(Map<String,Object> map) {
        this.setPageNum(MapUtils.getIntValue(map,"pageNum",1));
        this.setPageSize(MapUtils.getIntValue(map,"pageSize",10));
        this.setOrderColumn(MapUtils.getString(map,"orderColumn"));
        this.setOrderType(MapUtils.getString(map,"orderType"));
    }

}
